/**
 * 根据历史记录推送商品
 * */
var {
    express,
    db_mysql,
    errorData,
    strLen,
    historyProduct
} = require('./common-req.js')
var router = express.Router();
const { StringDecoder } = require('string_decoder');
const decoder = new StringDecoder('utf8');

// 根据属来分类
router.get('/:phone?', (req, res) => {
    const phone = req.params.phone;
    const pageNum = parseInt(req.query.pageNum) - 1 >= 0 ? parseInt(req.query.pageNum) - 1 : 0;
    const pageSize = parseInt(req.query.pageSize);
    let selSql = 'select * from historyview where phone=? limit 0, 50';
    let selArr = [phone];
    let resData = {
        data: [],
        msg: '获取推荐数据成功',
        status: 0,
        code: 200,
        total: 50
    }
    db_mysql.select(selSql, selArr, async results => {
        let data = await historyProduct(results, pageNum, pageSize);
        data.forEach(item => {
            let base = decoder.write(item.baseInfo);
            let grow = decoder.write(item.growInfo);
            let form = decoder.write(item.formInfo);
            let cul = decoder.write(item.cultivateInfo);
            var obj = {
                botanyId: item.botanyId,
                doorId: item.doorId,
                outlineId: item.outlineId,
                sectionId: item.sectionId,
                genusId: item.genusId,
                name: item.name,
                baseInfo: base === '' ? base : JSON.parse(base),
                growInfo: grow === '' ? grow : JSON.parse(grow),
                formInfo: form === '' ? form : JSON.parse(form),
                cultivateInfo: cul === '' ? cul : JSON.parse(cul),
                sectionId: item.sectionId,
                src: item.src,
                time: item.time
            }
            resData.data.push(obj);
        })
        res.send(resData)
    })
})


module.exports = router;
